//
//  ColoredLineChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "ColoredLineChartViewController.h"
@interface ColoredLineChartViewController ()
@end

@implementation ColoredLineChartViewController

NSMutableArray *chartViews;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}



-(void)initChartView{
    chartViews=[[NSMutableArray alloc]init];
    for (int i=0; i<4; i++) {
        LineChartView *temLineChartView=[[LineChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4+(kScreenH/8)*i, kScreenW/2, kScreenH/8)];
        
        [self.view addSubview:temLineChartView];
        [chartViews addObject:temLineChartView];
    }

    for (int i = 0; i < chartViews.count; i++)
    {
        LineChartData *data = [self setDataCount:36 range:100];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
        
        NSArray *colors = @[
                            [UIColor colorWithRed:137/255.f green:230/255.f blue:81/255.f alpha:1.f],
                            [UIColor colorWithRed:240/255.f green:240/255.f blue:30/255.f alpha:1.f],
                            [UIColor colorWithRed:89/255.f green:199/255.f blue:250/255.f alpha:1.f],
                            [UIColor colorWithRed:250/255.f green:104/255.f blue:104/255.f alpha:1.f],
                            ];
        
        [self setupChart:chartViews[i] data:data color:colors[i % colors.count]];
    }
}

- (void)setupChart:(LineChartView *)chart data:(LineChartData *)data color:(UIColor *)color
{
    [(LineChartDataSet *)[data getDataSetByIndex:0] setCircleHoleColor:color];
    
    chart.delegate = self;
    [self setupChartView:chart color:color];
    [self setChartParam:chart];
    [self setLegendStyle:chart];
    chart.data = data;
    [chart animateWithXAxisDuration:2.5];
}

- (void)setupChartView:(LineChartView *)chartView color:(UIColor *)color
{
    chartView.backgroundColor = color;
    
    chartView.descriptionText = @"";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.drawGridBackgroundEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = NO;
    [chartView setViewPortOffsetsWithLeft:10.0 top:0.0 right:10.0 bottom:0.0];
    
}

-(void)setChartParam:(LineChartView *)chartView{
    chartView.leftAxis.enabled = NO;
    chartView.rightAxis.enabled = NO;
    chartView.xAxis.enabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(LineChartView *)chartView{
    chartView.legend.enabled = NO;
}


-(LineChartData *)setDataCount:(int)count range:(double)range
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i % 12) stringValue]];
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double val = (double) (arc4random_uniform(range)) + 3;
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"DataSet 1"];
    
    set1.lineWidth = 1.75;
    set1.circleRadius = 5.0;
    set1.circleHoleRadius = 2.5f;
    [set1 setColor:UIColor.whiteColor];
    [set1 setCircleColor:UIColor.whiteColor];
    set1.highlightColor = UIColor.whiteColor;
    set1.drawValuesEnabled = NO;
    
    return [[LineChartData alloc] initWithXVals:xVals dataSet:set1];
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end